package com.ltv.xiangqiplugin.views;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Vector;

import javax.swing.JApplet;
import javax.swing.SwingUtilities;

import com.ltv.xiangqiplugin.models.Game;
import com.ltv.xiangqiplugin.models.MoveStack;
import com.ltv.xiangqiplugin.models.PositionStack;
import com.ltv.xiangqiplugin.utils.MoveUtils;

@SuppressWarnings("serial")
public class ChessApp extends JApplet {
	private static Vector<String> bufferedMoves;
	private static Vector<String> bufferedVnMoves;
	private static Vector<String> bufferedPositions;
	private static int bufferedIndex;
	
	@Override
	public void init() {
		//Execute a job on the event-dispatching thread; creating this applet's GUI.
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    try {
						createGUI();
					} catch (Exception e) {
						e.printStackTrace();
					}
                }
            });
        } catch (Exception e) { 
            e.printStackTrace();
        }
	}
	
	private void createGUI() throws Exception {
		this.getContentPane().setLayout(new GridBagLayout());
		
		Game game = new Game();
		BoardView.defaultBoardView().setGame(game);
		
		GridBagConstraints constrs = new GridBagConstraints();
		
		getContentPane().add(BoardView.defaultBoardView());
		
		constrs.fill = GridBagConstraints.BOTH;
	    constrs.gridwidth = 1;
	    constrs.gridheight = 1;
	    constrs.gridx = 0;
	    this.getContentPane().add(GameActionView.getInstance(), constrs);
	    
	    constrs.fill = GridBagConstraints.BOTH;
		constrs.weightx = 1.0;
	    constrs.gridx = GridBagConstraints.RELATIVE;
	    constrs.gridheight = 1;
	    constrs.fill = GridBagConstraints.BOTH;
	    constrs.gridwidth = GridBagConstraints.REMAINDER;
	    this.getContentPane().add(MoveStackView.getInstance(), constrs);
	    
	    String initPosition = this.getParameter("initPosition");
	    if (initPosition != null) {
	    	BoardView.defaultBoardView().getGame().setPosition(initPosition);
	    }
	    
	    String moveList = this.getParameter("moveList");
	    
	    if (moveList != null) {
		    StringBuffer moveListUpdated = new StringBuffer();
		    
		    for (int i = 0; i < moveList.length(); i++) {
		    	char c = moveList.charAt(i);
		    	if (isNum(c)) {
		    		moveListUpdated.append(Integer.parseInt("" + c) + 1);
		    	} else {
		    		moveListUpdated.append(c);
		    	}
		    }
		    
		    String moves[] = moveListUpdated.toString().toLowerCase().split("\\+");
		    
		    for (String move : moves) {
		    	String vnMove = MoveUtils.getVnMove(move, BoardView.defaultBoardView().getGame());
		    	if (BoardView.defaultBoardView().getGame().move(move, false)) {
			    	PositionStack.getInstance().addPostion(BoardView.defaultBoardView().getGame().getPosition());
					MoveStack.getInstance().addMove(move);
					MoveStack.getInstance().addVnMove(vnMove);
		    	}
		    }
		    MoveStack.getInstance().notifyObservers(true);
		    PositionStack.getInstance().first();
		    MoveStack.getInstance().first();
	    }
	    
	    bufferedPositions = new Vector<String>();
	    for (String position : PositionStack.getInstance().getPositions()) {
	    	bufferedPositions.add(position);
	    }
	    
	    bufferedMoves = new Vector<String>();
	    for (String move : MoveStack.getInstance().getMoves()) {
	    	bufferedMoves.add(move);
	    }
	    
	    bufferedVnMoves = new Vector<String>();
	    for (String vnMove : MoveStack.getInstance().getVnMoves()) {
	    	bufferedVnMoves.add(vnMove);
	    }
	    
	    bufferedIndex = 0;
	}
	
	private boolean isNum(char c) {
		return c >= '0' && c <= '9';
	}

	public static Vector<String> getBufferedMoves() {
		Vector<String> ret = new Vector<String>();
	    for (String move : bufferedMoves) {
	    	ret.add(move);
	    }
	    
		return ret;
	}
	
	public static Vector<String> getBufferedVnMoves() {
		Vector<String> ret = new Vector<String>();
	    for (String vnMove : bufferedVnMoves) {
	    	ret.add(vnMove);
	    }
	    
		return ret;
	}

	public static Vector<String> getBufferedPositions() {
		Vector<String> ret = new Vector<String>();
	    for (String position : bufferedPositions) {
	    	ret.add(position);
	    }
	    
		return ret;
	}

	public static int getBufferedIndex() {
		return bufferedIndex;
	}

	public static void setBufferedIndex(int bufferedIndex) {
		ChessApp.bufferedIndex = bufferedIndex;
	}
	
}
